<template>
  <div>
    <BasicTable @register="registerTable" :rowSelection="rowSelection">
      <template #tableTitle>
        <a-dropdown v-if="selectedRowKeys.length > 1">
          <template #overlay>
            <a-menu>
              <a-menu-item key="1" @click="batchHandleDelete">
                <Icon icon="ant-design:delete-outlined" />
                删除
              </a-menu-item>
            </a-menu>
          </template>
          <a-button
            >批量操作
            <Icon icon="ant-design:down-outlined" />
          </a-button>
        </a-dropdown>
      </template>
      <template #action="{ record }">
        <TableAction :actions="getActions(record)" />
      </template>
    </BasicTable>
    <PreviewDefineModal @register="registerPreviewModal" @success="handlePreviewSaveSuccess" />
    <StartProcessDrawer @register="registerStartProcessDrawer" />
  </div>
</template>
<script lang="ts" name="system-position" setup>
  import { ref } from 'vue';
  import { BasicTable, TableAction, ActionItem } from '/@/components/Table';
  import { useModal } from '/@/components/Modal';
  import { useDrawer } from '/@/components/Drawer';
  import { list, deleteProcessDefine, batchDeleteProcessDefine, upAndDownProcessDefine, getExportUrl, getImportUrl } from './processdefine.api';
  import { columns, searchFormSchema } from './processdefine.data';
  import PreviewDefineModal from './PreviewDefineModal.vue';
  import StartProcessDrawer from './StartProcessDrawer.vue';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { useListPage } from '/@/hooks/system/useListPage';

  const { createMessage } = useMessage();
  const [registerPreviewModal, { openModal: openPreviewModel }] = useModal();
  const [registerStartProcessDrawer, { openDrawer: openStartProcessDrawer }] = useDrawer();

  // 列表页面公共参数、方法
  const { prefixCls, onExportXls, onImportXls, tableContext } = useListPage({
    designScope: 'processDefine-template',
    tableProps: {
      title: '流程定义列表',
      api: list,
      columns: columns,
      formConfig: {
        schemas: searchFormSchema,
      },
      actionColumn: {
        width: 200,
      },
      showIndexColumn: true,
      beforeFetch: handleBeforeFetch,
    },
    exportConfig: {
      name: '流程设计列表',
      url: getExportUrl,
    },
    importConfig: {
      url: getImportUrl,
    },
  });

  const [registerTable, { reload, clearSelectedRowKeys }, { rowSelection, selectedRowKeys }] = tableContext;

  /**
   * 操作列定义
   * @param record
   */
  function getActions(record) {
    return [
      {
        label: '查看',
        onClick: handlePreview.bind(null, record),
        auth: ['admin', 'wf:processDefine:detail'], // 根据权限控制是否显示: 无权限，不显示
      },
      {
        label: '发起',
        disabled: record.state == 0,
        onClick: handleStartProcess.bind(null, record),
        auth: ['admin', 'wf:processDefine:startAndExecute'], // 根据权限控制是否显示: 有权限，会显示
      },
      {
        label: '启用',
        popConfirm: {
          title: '确定启用吗？',
          confirm: handleUpAndDown.bind(null, record),
        },
        ifShow() {
          return record.state == '0';
        },
        auth: ['admin', 'wf:processDefine:upAndDown'], // 根据权限控制是否显示: 有权限，会显示
      },
      {
        label: '禁用',
        popConfirm: {
          title: '确定禁用吗？',
          confirm: handleUpAndDown.bind(null, record),
        },
        ifShow() {
          return record.state == '1';
        },
        auth: ['admin', 'wf:processDefine:upAndDown'], // 根据权限控制是否显示: 有权限，会显示
      },
      {
        label: '删除',
        popConfirm: {
          title: '确定删除吗？',
          confirm: handleDelete.bind(null, record),
        },
        auth: ['admin', 'wf:processDefine:remove'], // 根据权限控制是否显示: 有权限，会显示
      },
    ];
  }

  /**
   * 批量删除事件
   */
  async function batchHandleDelete() {
    await batchDeleteProcessDefine({ ids: selectedRowKeys.value.join(',') }, () => {
      selectedRowKeys.value = [];
      reload();
    });
  }

  // 删除
  async function handleDelete(record) {
    await deleteProcessDefine({ id: record.id }, () => {
      selectedRowKeys.value = [];
      reload();
    });
  }

  // 启用/禁用
  function handleUpAndDown(record) {
    const ids: Array<any> = [];
    if (record && record.id) {
      ids.push(record.id);
    }
    upAndDownProcessDefine({
      ids: ids,
      opType: record.state == '1' ? '0' : '1',
    }).then(() => {
      createMessage.success(`${record.state == 1 ? '禁用' : '启用'}成功`);
      clearSelectedRowKeys();
      reload();
    });
  }

  // 处理查询请求参数
  function handleBeforeFetch(params) {
    if (params) {
      Object.keys(params).forEach((key) => {
        if (params[key] == 9999999999) {
          // 删除全部自定义的值-不提交到后端
          delete params[key];
        }
      });
    }
    return params;
  }

  /**
   * 打开流程预览
   * @param {} record
   */
  function handlePreview(record) {
    openPreviewModel(true, {
      record,
    });
  }

  // 流程设计保存成功，刷新列表
  function handlePreviewSaveSuccess() {
    reload();
  }

  /**
   * 打开发起流程
   * @param {} record
   */
  function handleStartProcess(record) {
    openStartProcessDrawer(true, {
      record,
    });
  }
</script>
